#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <boost/filesystem.hpp>
#include <iostream>

using namespace std;
using namespace cv;




int main(int argc, char* argv[]){
    std::string path="/home/wya/Project/Dataset/Database/polarized/all_sequences/MVS/2/images";
    std::string Images_path = path+"/*.png";
    std::vector<cv::String> images;

    boost::filesystem::path myPath(path);
    boost::filesystem::directory_iterator endIter;
    int a=0;
    for (boost::filesystem::directory_iterator iter(myPath); iter != endIter; iter++) {
        if (boost::filesystem::is_directory(*iter)) {
            cout << "is dir" << endl;
            cout << iter->path().string() << endl;
            cv::glob(iter->path().string()+"/*.png", images);

            Mat img0= imread(images[0],IMREAD_UNCHANGED);
            Mat AverageImage = Mat::zeros(img0.rows,img0.cols, CV_32F);
            accumulate(img0, AverageImage);
            for (int i =1; i < images.size(); ++i) {
                Mat img= imread(images[i],IMREAD_UNCHANGED);
                accumulate(img, AverageImage);
            }
            AverageImage /= images.size();
            AverageImage.convertTo(AverageImage, CV_8U);
//            imshow("img_mean"+ to_string(a),AverageImage);
            imwrite(path+"/"+iter->path().string().substr(iter->path().string().rfind('/')+1)+"_mean.png",AverageImage);
            a++;
            images.clear();
        } else {
            cout << "is a file" << endl;
            cout << iter->path().string() << endl;
        }
    }
    cvWaitKey(0);


//    Mat img= imread(path+"/dist.png",IMREAD_UNCHANGED);
//    imshow("img",img);
//    Mat phi_gary,phi_color;
//    img.convertTo(phi_gary,CV_8UC1,5);
//    applyColorMap(phi_gary, phi_color, COLORMAP_JET);
//
//    imshow("dist",phi_color);
//    imwrite(path+"/dist_8bit.png",phi_gary);
//    cvWaitKey(0);

}
